from tkinter import Tk
from tkinter import ttk
from database.table import tablee
from database.table.tablee import Purchases,Commodity
import tkinter as tk

def SearchPurchase():
    search_purchase_screen = Tk()
    search_purchase_screen.title('查询采购信息')
    screen_width, screen_height = search_purchase_screen.maxsize()
    # 窗口的宽和高
    width = 605
    height = 200
    centre = '%dx%d+%d+%d' % (width, height, (screen_width - width) / 2,
                              (screen_height - height) / 2)
    search_purchase_screen.geometry(centre)
    search_purchase_screen.resizable(0, 0)

    label = tk.Label(search_purchase_screen, text='请输入要查询的采购信息编号：', font=('楷体', 14))
    label.place(x=150, y=20)
    # 编号标签文本框
    user_pno_label = tk.Label(search_purchase_screen, text='编号', font=('楷体', 20))
    user_pno_label.place(x=150, y=60)
    user_pno_text = tk.StringVar()
    user_pno_entry = tk.Entry(search_purchase_screen, textvariable=user_pno_text,
                              width=15)
    user_pno_entry.place(x=230, y=68)

    # 确定按钮响应事件
    def confirm():
        id = str(user_pno_entry.get())
        purchasee = tablee.session.query(Purchases).filter(Purchases.Pno == id).all()
        data = Purchases.to_json(purchasee)

        length = len(data)

        if length == 1:  # 查找的采购信息编号存在
            search = tk.Toplevel(search_purchase_screen)
            search.title(id + '员工信息')
            screen_width, screen_height = search.maxsize()
            # 窗口的宽和高
            width = 700
            height = 100
            centre = '%dx%d+%d+%d' % (width, height, (screen_width - width) / 2,
                                      (screen_height - height) / 2)
            search.geometry(centre)

            tree = ttk.Treeview(search)  # 创建表格对象
            tree["columns"] = ("编号", "员工号", "商品号", "单价", "数量", "总价","时间", "备注")
            tree.column("编号", width=50)  # 设置列
            tree.column("员工号", width=50)
            tree.column("商品号", width=50)
            tree.column("单价", width=50)
            tree.column("数量", width=100)
            tree.column("总价", width=50)
            tree.column("时间",width = 100)
            tree.column("备注", width=50)
            tree.heading("编号", text="编号")  # 显示表头名
            tree.heading("员工号", text="员工号")
            tree.heading("商品号", text="商品号")
            tree.heading("单价", text="单价")
            tree.heading("数量", text="数量")
            tree.heading("总价", text="总价")
            tree.heading("时间", text="时间")
            tree.heading("备注", text="备注")

            cnum = data[0]['Cno']
            comm = tablee.session.query(Commodity).filter(Commodity.Cno == cnum).all()
            commdityy = Commodity.to_json(comm)
            unitprice = commdityy[0]['Cunitprice']

            for i in range(0, length):
                pno = data[i]['Pno']
                eno = data[i]['Eno']
                cno = data[i]['Cno']
                num = data[i]['Pnum']
                time = data[i]['Pdate']
                remarks = data[i]['Premarks']
                totalp = int(num) * int(unitprice)

                tree.insert("", 0, text='', value=(pno, eno, cno, unitprice ,num,str(totalp), time, remarks))
            tree.pack()

            tablee.session.close()

        elif id == '':
            search = tk.Toplevel(search_purchase_screen)
            search.title("Error")
            screen_width, screen_height = search.maxsize()
            # 窗口的宽和高
            width = 400
            height = 100
            centre = '%dx%d+%d+%d' % (width, height, (screen_width - width) / 2,
                                      (screen_height - height) / 2)
            search.geometry(centre)
            label = tk.Label(search, text='编号不能为空，请重新输入！', font=('楷体', 14))
            label.place(x=60, y=15)

            def confirmm():
                search.destroy()

            # 确定按钮
            user_confirm_button = tk.Button(search, text='确定', font=('楷体', 12),
                                            width=10, command=confirmm)
            user_confirm_button.place(x=140, y=60)
        else:  # 采购信息编号不存在
            search = tk.Toplevel(search_purchase_screen)
            search.title("Error")
            screen_width, screen_height = search.maxsize()
            # 窗口的宽和高
            width = 400
            height = 100
            centre = '%dx%d+%d+%d' % (width, height, (screen_width - width) / 2,
                                      (screen_height - height) / 2)
            search.geometry(centre)
            label = tk.Label(search, text='此编号不存在,请重新输入！', font=('楷体', 14))
            label.place(x=60, y=15)

            def confirmm():
                search.destroy()

            # 确定按钮
            user_confirm_button = tk.Button(search, text='确定', font=('楷体', 12),
                                            width=10, command=confirmm)
            user_confirm_button.place(x=140, y=60)

    # 返回按钮响应事件
    def back():
        search_purchase_screen.destroy()

    # 确定按钮
    user_confirm_button = tk.Button(search_purchase_screen, text='确定', font=('楷体', 12),
                                    width=10, command=confirm)
    user_confirm_button.place(x=150, y=120)
    # 返回按钮
    user_back_button = tk.Button(search_purchase_screen, text='返回', font=('楷体', 12),
                                 width=10, command=back)
    user_back_button.place(x=280, y=120)

    search_purchase_screen.mainloop()


if __name__ == '__main__':
    SearchPurchase()